﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public abstract class SearchItem : System.Windows.Controls.Grid
	{
		#region 控件无关

		//[System.ComponentModel.Description("显示的名称")]
		//public string Name { get; set; }

		[System.ComponentModel.Description("搜索的字段")]
		public string Field
		{
			get
			{
				if (this.Fields == null || this.Fields.Length == 0)
					return null;
				return this.Fields[0];
			}
			set
			{
				this.Fields = new string[] { value };
			}
		}

		[System.ComponentModel.TypeConverter(typeof(StringArrayConverter))]
		public string[] Fields { get; set; }

		[System.ComponentModel.Description("是否在简单搜索中显示")]
		public bool ShowInSimpleSearcher
		{
			get
			{
				return this.Visibility == System.Windows.Visibility.Visible;
			}
			set
			{
				this.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
			}
		}

		private bool _ShowInAdvanceSearcher = true;
		[System.ComponentModel.Description("是否在高级搜索中显示")]
		public bool ShowInAdvanceSearcher
		{
			get
			{
				return this._ShowInAdvanceSearcher;
			}
			set
			{
				this._ShowInAdvanceSearcher = value;
			}
		}

		//private System.Web.UI.WebControls.Table _SimpleSearcherLayout;
		//public System.Web.UI.WebControls.Table SimpleSearcherLayout
		//{
		//    get
		//    {
		//        if (this._SimpleSearcherLayout == null)
		//        {
		//            this._SimpleSearcherLayout = new System.Web.UI.WebControls.Table();
		//            this._SimpleSearcherLayout.CellPadding = 0;
		//            this._SimpleSearcherLayout.CellSpacing = 0;

		//            System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();

		//            System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
		//            cell.Text = this.Name;
		//            row.Cells.Add(cell);

		//            cell = new System.Web.UI.WebControls.TableCell();
		//            cell.Controls.Add(this.SimpleSearcherInput);
		//            row.Cells.Add(cell);

		//            this._SimpleSearcherLayout.Rows.Add(row);
		//        }
		//        return this._SimpleSearcherLayout;
		//    }
		//}

		public abstract System.Windows.FrameworkElement SimpleSearcherInput { get; }

		public virtual double SimpleSearcherWidth
		{
		    get
		    {
				return this.SimpleSearcherInput.Width;
		    }
			set
			{
				this.SimpleSearcherInput.Width = value;
			}
		}

		public abstract Operator[] Operators { get; }

		public abstract System.Windows.FrameworkElement AdvanceSearcherInput { get; }

		public virtual object GetInputValue(System.Windows.FrameworkElement searcherInput)
		{
			return searcherInput.GetType().GetProperty(this.InputValueProperty).GetValue(searcherInput, null);
		}

		public virtual void SetInputValue(System.Windows.FrameworkElement searcherInput, object value)
		{
			searcherInput.GetType().GetProperty(this.InputValueProperty).SetValue(searcherInput, value, null);
		}

		public virtual string InputValueProperty
		{
			get
			{
				return "Text";
			}
		}

		//protected abstract OperatorItem[] AdvanceSearcherOperatorItems { get; }

		public virtual Sunny.Controls.Searcher.WhereCommand GetCommand(string field, string expression, int index, System.Windows.FrameworkElement searcherInput)
		{
			//string command = null;
			//System.Collections.ArrayList parameters = new System.Collections.ArrayList();
			//foreach (string field in this.Fields)
			//{
			//    if (command != null)
			//        command += "||";
			//    if (this.Fields.Length > 1)
			//        command += "(";
			//    command += string.Format(expression, this.Field, "@" + index++);
			//    if (this.Fields.Length > 1)
			//        command += ")";
			//    parameters.Add(this.GetInputValue(searcherInput));
			//}
			//return new Searcher.WhereCommand { Text = command, Parameters = parameters.ToArray() };
			return new Searcher.WhereCommand { Text = string.Format(expression, this.Field, "@" + index), Parameters = new object[] { this.GetInputValue(searcherInput) } };
		}
		
		#endregion

		#region 控件相关
		private System.Windows.Controls.TextBlock _NameCell;
		protected System.Windows.Controls.TextBlock NameCell
		{
			get
			{
				if (this._NameCell == null)
				{
					this._NameCell = new System.Windows.Controls.TextBlock();
					this._NameCell.VerticalAlignment = System.Windows.VerticalAlignment.Center;
					//this._NameCell.BorderThickness = new System.Windows.Thickness(0);
					//this._NameCell.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
				}
				return this._NameCell;
			}
		}

		//private System.Windows.Controls.StackPanel _InputCell;
		//protected System.Windows.Controls.StackPanel InputCell
		//{
		//	get
		//	{
		//		if (this._InputCell == null)
		//		{
		//			this._InputCell = new System.Windows.Controls.StackPanel();
		//			//System.Windows.Controls.Grid.SetRow(this._InputCell, 1);
		//			System.Windows.Controls.Grid.SetColumn(this._InputCell, 1);
		//			if (this.SimpleSearcherInput != null)
		//				this._InputCell.Children.Add(this.SimpleSearcherInput);
		//		}
		//		return this._InputCell;
		//	}
		//}

		//private System.Web.UI.WebControls.TableCell _InputCell;
		//protected System.Web.UI.WebControls.TableCell InputCell
		//{
		//    get
		//    {
		//        if (this._InputCell == null)
		//            this._InputCell = new System.Web.UI.WebControls.TableCell();
		//        return this._InputCell;
		//    }
		//}

		public SearchItem()
		{
			this.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
			this.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

			this.Margin = new System.Windows.Thickness(5, 0, 0, 0);

			this.Children.Add(this.NameCell);

			System.Windows.Controls.Grid.SetColumn(this.SimpleSearcherInput, 1);
			this.Children.Add(this.SimpleSearcherInput);
			//System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
			//cell.Controls.Add(this.SimpleSearcherInput);
			//row.Cells.Add(cell);
		}

		//protected override void CreateChildControls()
		//{
		//    this.InputCell.Controls.Add(this.Input);
		//    base.CreateChildControls();
		//}

		public string Title
		{
			get
			{
				//string name = (string)this.NameCell.Text;
				//if (name == null)
				//	return null;
				return this.NameCell.Text.Substring(0, this.NameCell.Text.Length - 1);
			}
			set
			{
				this.NameCell.Text = value + "：";
			}
		}

		public SearchItem(string name)
			: this()
		{
			this.Title = name;
		}

		//public abstract System.Web.UI.Control Input { get; }

		public abstract Sunny.Controls.Searcher.WhereCommand GetCommand(string field, int index);

		public abstract void Empty();
		
		#endregion
	}
}